<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        h1 {
            color: #063;
            font-weight: bold;
            font-family: "黑体";
            text-align: center;
        }

        .datalist {
            border: 1px solid #007108;
            background-color: #d9ffdc;
            font-size: 14px;
            border-collapse: collapse;
            width: 800px;
            margin: 0px auto;
        }

        .datalist td {
            border: 1px solid #007108;
            text-align: center;
            line-height: 20px;
            padding: 4px 10px;
        }

        .bg {
            background-color: pink;
        }
    </style>

</head>

<body>
    <h1>学生信息表</h1>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="datalist">
        <thead>
            <tr>
                <td><input type="checkbox" /></td>
                <td>学号</td>
                <td>姓名</td>
                <td>性别</td>
                <td>年龄</td>
                <td>电话号码</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" /></td>
                <td>20141211</td>
                <td>黄小桂</td>
                <td>女</td>
                <td>18</td>
                <td>13882424465</td>
            </tr>
            <tr>
                <td><input type="checkbox" /></td>
                <td>20141212</td>
                <td>陈永亮</td>
                <td>男</td>
                <td>18</td>
                <td>13882424466</td>
            </tr>
            <tr>
                <td><input type="checkbox" /></td>
                <td>20141214</td>
                <td>张晓佳</td>
                <td>女</td>
                <td>18</td>
                <td>13882424468</td>
            </tr>
            <tr>
                <td><input type="checkbox" /></td>
                <td>20141215</td>
                <td>林德佳</td>
                <td>男</td>
                <td>18</td>
                <td>13882424469</td>
            </tr>
        </tbody>
    </table>
    <script>
        var trs = document.querySelector('tbody').querySelectorAll('tr');
        for (var i = 0; i < trs.length; i++) {
            trs[i].onmouseover = function () {
                this.className = 'bg';
            }
            trs[i].onmouseout = function () {
                this.className = '';
            }
        }

        var inp1 = document.querySelector('thead').querySelector('input')
        var inp2 = document.querySelector('tbody').querySelectorAll('input')
        inp1.onclick = function () {
            for (var i = 0; i < inp2.length; i++) {
                // if (inp2[i].checked == true) {
                //     inp2[i].checked = false;
                // }
                // else {
                //     inp2[i].checked = true;
                // }
                inp2[i].checked = this.checked
            }
        }
        for (var i = 0; i < inp2.length; i++) {
            inp2[i].onclick = function () {
                //flag 控制全选按钮 是否被选中
                var flag = true
                // 每次点击下面的复选框都要循环检查下面所有的按钮是否全部被选中
                for (var i = 0; i < inp2.length; i++) {
                    // 判断 inp2 是否不是全部被选中
                    if (!inp2[i].checked) {
                        // 如果全部不是全部被选中 则flag 为false
                        flag = false
                    }
                }
                // 让inp1 的checked 为flag 的值
                inp1.checked = flag
            }
        }

    </script>
</body>

</html>